package com.extweb4j.web.controller;

import java.util.List;
import java.util.Map;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import cn.dreampie.mail.Mailer;

import com.extweb4j.core.controller.ExtController;
import com.extweb4j.core.kit.ExtKit;
import com.extweb4j.core.kit.SMSKit;
import com.extweb4j.core.model.ExtJob;
import com.extweb4j.core.model.ExtJobUser;
import com.extweb4j.core.model.ExtUser;

public class QuartzController extends ExtController implements Job{
	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		
		JobDataMap dataMap = context.getJobDetail().getJobDataMap();
		String job_id = dataMap.getString("id");
		System.out.println("任务启动了"+job_id);
		//获得任务信息
//		ExtJob job = ExtJob.dao.findById(job_id);
//		String content = job.getStr("job_content");
//		System.out.println(content);
//		
//		//拼凑邮箱地址，手机号码组
//		List<ExtUser> users = ExtJobUser.dao.findUserByJobId(job_id);
//		Map<String,List<String>> map = SMSKit.getPhonesAndEmails(users);
//		List<String> phones = map.get("phones");
//		String[] emails = ExtKit.PraseListToStringArray(map.get("emails"));
//		
//		try {
//			//发送邮件
//			Mailer.sendHtml(job.getStr("job_name"),content,emails);
//			//发送短信
//			SMSKit.batchSend(phones, content);
//			job.set("status", JobStatus.执行成功.getState());	
//			
//		} catch (Exception e) {	
//			job.set("status", JobStatus.执行失败.getState());
//			error(e.toString());
//			
//		}finally{
//			job.update();
//		}
	}
	
	public String getSignature(String group){
		String Signature = "";
		switch(group){
			case "default": Signature="日常提醒:"; break;
			case "meeting": Signature="会议通知:"; break;
			case "exam":    Signature="监考提醒:"; break;
		}
		return Signature;
	}
}
